package array

func firstMissingPositive(nums []int) int {
	for i, v := range nums {
		for v != i+1 {
			if v <= 0 || v > len(nums) || v == nums[v-1] {
				break
			}
			idx := v - 1
			v = nums[idx]
			nums[idx] = v
		}
	}
	for i, v := range nums {
		if v != i+1 {
			return i + 1
		}
	}
	return len(nums) + 1
}
